<?php
	function TwitterHttp($method, $url, $oauth_param, $secret, $qurry_param=NULL, $post_param=NULL)//3个param数组传入前需要urlencode
	{
		$oauth_param['oauth_signature']=urlencode(GetSign($method, $url, $oauth_param, $secret, $qurry_param, $post_param));

		if($qurry_param != NULL)
		{
			$url .= '?';
			foreach ($qurry_param as $key => $val)
				$url .= $key . '=' . $val . '&';
			$url=substr($url, 0, -1);
		}

		return TwitterHttpReq($method, $url,  $post_param, array(GetAuthHeader($oauth_param)));
	}

	function GetSign($method, $url, $oauth_param, $secret, $qurry_param=NULL, $post_param=NULL)//3个param数组传入前需要urlencode
	{
		$sign_param=$oauth_param;
		if($qurry_param != NULL)
			foreach($qurry_param as $key => $val)
				$sign_param[$key]=$val;
		if($post_param != NULL)
			foreach($post_param as $key => $val)
				$sign_param[$key]=$val;
		ksort($sign_param);

		$sign_base=$method . '&' . urlencode($url) . '&';
		foreach($sign_param as $key => $val )
			$sign_base .= urlencode($key) . '%3D' . urlencode($val) . '%26';
		$sign_base = substr($sign_base, 0, -3);
		$oauth_signature=base64_encode(hash_hmac('sha1', $sign_base, $secret, true));

		return $oauth_signature;
	}

	function GetAuthHeader($oauth_param)
	{
		$header_auth='Authorization: OAuth ';
		foreach($oauth_param as $key => $val )
			$header_auth .= $key . '="' . $val . '", ';
		$header_auth=substr($header_auth, 0, -2);

		return $header_auth;
	}

	function TwitterHttpReq($method, $url,  $post_param=NULL, $http_header=NULL)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		if($method == 'POST')
		{
			curl_setopt($ch, CURLOPT_POST, 1);
			$http_header[] = 'Expect:';
			if($post_param != NULL)
			{
				$post_body='';
				foreach($post_param as $key => $val)
					$post_body .= $key . '=' . $val . '&';
				$post_body=substr($post_body, 0, -1);
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_body);
			}
			else
				$http_header[] = 'Content-Length: 0';
		}
		if($http_header != NULL)
			curl_setopt($ch, CURLOPT_HTTPHEADER, $http_header);
		$response['content']=curl_exec($ch);
		if($response['content']!=FALSE)
		{
			$response['http_code']=curl_getinfo($ch, CURLINFO_HTTP_CODE);
			$response['content_type']=curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
		}
		curl_close($ch);

		return $response;
	}
?>